package com.hsy.linemoney.modules.common;

import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.deparse.dpframe.ui.common.DPBusEvent;
import com.hsy.linemoney.common.Consts;
import com.hsy.linemoney.modules.exchange.ExchangeSuccessActivity;
import com.hsy.linemoney.modules.tender.activity.TenderSuccessActivity;

/**
 * Created by MartinKent on 2016/9/27.
 */
public class ProcessPayActivity extends CommonBaseActivityWithSimpleActionBar {
    private WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webView = new WebView(this);
        setContentView(webView);
        setTitle("");

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setAppCacheEnabled(false);
        settings.setSaveFormData(false);
        settings.setSavePassword(false);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setTitle(null == title ? "" : title.trim());
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (!TextUtils.isEmpty(url) && url.contains("LmBusinessServices-portlet.payment/process-transaction")) {
                    DPBusEvent event = new DPBusEvent();
                    event.what = Consts.BusEventCommands.FINISH_EXCHANGE_STEP_2_AND_3;
                    postBusEvent(event);
                    finish();
                    if ("0".equals(getActivityStartExtra()[1])) {
                        startActivity(ExchangeSuccessActivity.class);
                    } else if ("1".equals(getActivityStartExtra()[1])) {
                        startActivity(TenderSuccessActivity.class);
                    }
                } else {
                    view.loadUrl(url);
                }
                return true;
            }
        });

        webView.loadUrl(getActivityStartExtra()[0]);
    }
}
